package com.ng.desktop.expansionsa.view.alquiler;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.JComboBox;

import com.ng.desktop.expansionsa.model.Alquiler;
import com.ng.framework.persistence.NGSearchParameter;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.view.desktop.filters.NGFilter;

public class FilterNotificacion extends NGFilter {
	
	private static final long serialVersionUID = 6534382511836732894L;
	
	private final DateFormat FORMATO_FECHA = new SimpleDateFormat("dd/MM/yyyy");
	
	
	private JComboBox comboBox;

	public FilterNotificacion() {
		super("Alerta");
		
		this.buildGui();
	}
	
	private void buildGui() {
		this.setLayout(new GridBagLayout());
		
		this.add(this.getComboBox(), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
	}
	
	protected JComboBox getComboBox() {
		if(comboBox == null) {
			this.comboBox = new JComboBox(new Opcion [] { null, Opcion.SI, Opcion.NO });
		}
		return comboBox;
	}

	@Override
	public List<NGSearchParameter> getSearchParameters() {
		List<NGSearchParameter> searchParameters = null;
		
		if(this.getOpcionSeleccionada() == Opcion.SI) {
			searchParameters = Arrays.asList(new NGSearchParameter(Alquiler.class, "detalles", FORMATO_FECHA.format(new Date()), Type.DATE, UnaryOperation.SMALLER_THAN, true, "fechaDevolucion"));
		}
		
		return searchParameters;
	}
	
	public Opcion getOpcionSeleccionada() {
		return (Opcion)this.getComboBox().getSelectedItem();
	}

	@Override
	public void clear() {
		comboBox.setSelectedItem(null);
	}
	
	public enum Opcion {
		
		SI("Si"), NO("No");
		
		private String descripcion;
		
		Opcion(String descripcion) {
			this.descripcion = descripcion;
		}
		
		@Override
		public String toString() {
			return descripcion;
		}
	}
}